﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Xml;
using System.Diagnostics;
using System.Collections.Generic;
using System.Drawing;
using Microsoft.Ink;

namespace MokkAnnotator
{
    public static class FormSerialisor
    {

        public static void Serialise(List<TransparentRichTextBox> _listComment, string XmlFileName)
        {
            XmlTextWriter xmlSerialisedForm = new XmlTextWriter(XmlFileName, System.Text.Encoding.Default);
            xmlSerialisedForm.Formatting = Formatting.Indented;
            xmlSerialisedForm.WriteStartDocument();
            xmlSerialisedForm.WriteStartElement("TxtBoxs");
            for (int i = 0; i < _listComment.Count; i++)
            {
                if (_listComment[i].Text != "")
                {
                    xmlSerialisedForm.WriteStartElement("TxtBox" + i.ToString());

                    xmlSerialisedForm.WriteElementString("Name", i.ToString());
                    xmlSerialisedForm.WriteElementString("StartX", (_listComment[i].Location.X).ToString());
                    xmlSerialisedForm.WriteElementString("StartY", (_listComment[i].Location.Y).ToString());
                    xmlSerialisedForm.WriteElementString("Width", (_listComment[i].Width).ToString());
                    xmlSerialisedForm.WriteElementString("Height", (_listComment[i].Height).ToString());
                    _listComment[i].SaveFile(Application.StartupPath + "\\" + i, RichTextBoxStreamType.RichText);
                    xmlSerialisedForm.WriteEndElement();
                }
            }
            xmlSerialisedForm.WriteEndElement();
            xmlSerialisedForm.WriteEndDocument();
            xmlSerialisedForm.Close();
        }

        public static List<TransparentRichTextBox> Deserialise(string XmlFileName)
        {
            List<TransparentRichTextBox> _listComment = new List<TransparentRichTextBox>();
            if (File.Exists(XmlFileName))
            {
                XmlDocument xmlSerialisedForm = new XmlDocument();
                xmlSerialisedForm.Load(XmlFileName);
                XmlNode topLevel = xmlSerialisedForm.ChildNodes[1];
                foreach (XmlNode n in topLevel)
                {
                    TransparentRichTextBox commentTextBox = new TransparentRichTextBox();
                    commentTextBox.Height = Convert.ToInt32(n["Height"].InnerText);
                    commentTextBox.Width = Convert.ToInt32(n["Width"].InnerText);
                    commentTextBox.Location = new Point(Convert.ToInt32(n["StartX"].InnerText), Convert.ToInt32(n["StartY"].InnerText));
                    commentTextBox.BorderStyle = BorderStyle.None;
                    commentTextBox.LoadFile(Application.StartupPath + "\\" + n["Name"].InnerText);
                    _listComment.Add(commentTextBox);
                }
            }
            return _listComment;
        }
    }
}
